#include "plist.h"

#include "WarplaneConfig.h"

WarplaneConfig::PTR gWarplaneConfig;

bool WarplaneConfig::getUpgradeLevelNeedExp(int8_t level, int32_t& exp) const
{
    auto it = mUpgradeLevelNeedExp.find(level);
    if (it != mUpgradeLevelNeedExp.end())
    {
        exp = it->second;
        return true;
    }
    else
    {
        return false;
    }
}

bool WarplaneConfig::getItemAddExp(int16_t itemID, int32_t& exp) const
{
    auto it = mExpItemConfig.find(itemID);
    if (it != mExpItemConfig.end())
    {
        exp = it->second;
        return true;
    }
    else
    {
        return false;
    }
}

bool WarplaneConfig::getStarLevelMaxLevel(int8_t starLevel, int8_t& normalLevel) const
{
    auto it = mStarLevelNormalLevelMax.find(starLevel);
    if (it != mStarLevelNormalLevelMax.end())
    {
        normalLevel = it->second;
        return true;
    }
    else
    {
        return false;
    }
}

bool WarplaneConfig::getUpgradeStepConfig(int8_t step, UpgradeWarplaneStepConfig& config) const
{
    auto it = mUpgradeWarplaneStepConfig.find(step);
    if (it != mUpgradeWarplaneStepConfig.end())
    {
        config = it->second;
        return true;
    }
    else
    {
        return false;
    }
}

bool WarplaneConfig::getSplitWarplaneResource(int8_t starLevel, int32_t& resValue) const
{
    auto it = mStarLevelSplitResource.find(starLevel);
    if (it != mStarLevelSplitResource.end())
    {
        resValue = it->second;
        return true;
    }
    else
    {
        return false;
    }
}

void WarplaneConfig::loadAllConfig()
{
    try
    {
        AnyValue* pv = parsePlistByFileName("UpgradeWarplaneStarLevel.plist");
        if (pv != nullptr)
        {
            auto dict = pv->asDict();
            for (auto v : dict->getValue())
            {
                UpgradeWarplaneStepConfig oneStepConfig;

                int starLevel = 0;
                sscanf(v.first.c_str(), "%d", &starLevel);

                const PntDict* config = v.second->asDict();
                const PntDict::VALUE_TYPE& outherValue = config->getValue();
                oneStepConfig.needLevel = outherValue.find("NeedNormalLevel")->second->asInt()->getValue();
                oneStepConfig.endLevel = outherValue.find("ResetNormalLevel")->second->asInt()->getValue();

                const PntArray* needWarplanes = outherValue.find("NeedWarplanes")->second->asArray();

                for (auto v : needWarplanes->getValue())
                {
                    const PntDict::VALUE_TYPE& dictValue = v->asDict()->getValue();

                    oneStepConfig.needWarplanes.push_back({ dictValue.find("StarLevel")->second->asInt()->getValue(), 
                                                          dictValue.find("Type")->second->asInt()->getValue() });
                }

                mUpgradeWarplaneStepConfig[starLevel] = oneStepConfig;
            }
            delete pv;
        }

        pv = parsePlistByFileName("UpgradeWarplaneNormalLevel.plist");
        if (pv != nullptr)
        {
            auto rootDict = pv->asDict();
            {
                auto StarLevelMaxNormalLevelConfig = rootDict->getValue().find("StarLevelMaxNormalLevel")->second->asDict();
                auto dict = StarLevelMaxNormalLevelConfig->asDict();
                for (auto v : dict->getValue())
                {
                    int starLevel = 0;
                    sscanf(v.first.c_str(), "%d", &starLevel);
                    mStarLevelNormalLevelMax[starLevel] = v.second->asInt()->getValue();
                }
            }

            {
                auto NormalLevelExpConfig = rootDict->getValue().find("NormalLevelExp")->second->asDict();
                auto dict = NormalLevelExpConfig->asDict();
                for (auto v : dict->getValue())
                {
                    int normalLevel = 0;
                    sscanf(v.first.c_str(), "%d", &normalLevel);
                    mUpgradeLevelNeedExp[normalLevel] = v.second->asInt()->getValue();
                }
            }

            {
                auto ItemExpConfig = rootDict->getValue().find("ItemExp")->second->asDict();
                auto dict = ItemExpConfig->asDict();
                for (auto v : dict->getValue())
                {
                    int itemID = 0;
                    sscanf(v.first.c_str(), "%d", &itemID);
                    mExpItemConfig[itemID] = v.second->asInt()->getValue();
                }
            }
        }

        pv = parsePlistByFileName("SplitWarplane.plist");
        if (pv != nullptr)
        {
            auto dict = pv->asDict();
            for (auto v : dict->getValue())
            {
                int starLevel = 0;
                sscanf(v.first.c_str(), "%d", &starLevel);
                mStarLevelSplitResource[starLevel] = v.second->asInt()->getValue();
            }
        }
    }
    catch (PlistParseError& e)
    {

    }
}